dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.RadioButton");
dojo.require("dijit.Tooltip");
dojo.require("dojo.parser"); // scan page for widgets

dojo.addOnLoad( function()
{
	// hide loading gif
	window.setTimeout(function()
	{
		document.getElementById('img').style.display='none';
		document.getElementById('form').style.visibility='';
	}, 100);
});

var check_corrent_input = function()
{
	var x = dijit.byId('form_systemAccount').attr('value');
	if (
		! x.match(/^(.+)@(.{2,})\.(.{2,4})$/i)
		|| dijit.byId('form_password').attr('value') == ''
	) {
		return false;
	}
	return true;
};

var submit_btn = function()
{
	if (!check_corrent_input())
	{
		dialog_error.show();
		return;
	}
	dialog_wait.show();
	dojo.xhrGet({
		url: "html?action=check&mdl=register",
		content: {
			'systemAccount': dijit.byId('form_systemAccount').attr('value'),
			'pwd': dijit.byId('form_password').attr('value')
		},
		handleAs: "text",
		load: function(rdata, ioArgs) {
			if (rdata == 'false')
			{
				dojo.byId('sys1').style.backgroundColor = '#d00';
				dojo.byId('sys2').style.backgroundColor = '#d00';
				dojo.byId('error_check').style.display = '';
				dialog_wait.hide();
			} else if (rdata == 'true')
			{
				dojo.byId('sys1').style.backgroundColor = '';
				dojo.byId('sys2').style.backgroundColor = '';
				dojo.byId('error_check').style.display = 'none';
				submit_data();
			} else
			{
				alert ("Error while saving data. Please try again later. ("+rdata+")");
				dialog_wait.hide();
			}
	        return rdata;
	    },
	    error: function(response, ioArgs) {
	    	alert ("Error while saving data. Please try again later. ("+arguments[0]+")");
	    	dialog_wait.hide();
	        return response;
	    }
	});
};

var submit_data = function()
{
	dojo.xhrPost({
		url: "html?action=move&mdl=account_move",
		content: {
			'systemAccount': dijit.byId('form_systemAccount').attr('value'),
			'pwd': dijit.byId('form_password').attr('value')
		},
	    handleAs: "text",
	    // timeout: 30000, infinity
	    load: function(rdata, ioArgs) {
			dialog_wait.hide();
			if (rdata == 'test_cal_failed')
			{
				// movement check failed
				alert (lang_move_check_failed);
			} else if (rdata != 'null')
			{
				alert ("Error while saving data. Please try again later. ("+rdata+")");
			} else
			{
				// successful
				dojo.byId('form').style.display = 'none';
				dojo.byId('finished').style.display = 'block';
				if (typeof (window.opener) != "undefined")
				{
					window.opener.location.reload();
				}
			}
	        return rdata;
	    },
	    error: function(response, ioArgs) {
	    	alert ("Error while saving data. Please try again later. ("+arguments[0]+")");
	    	dialog_wait.hide();
	        return response;
	    }
	});
};

// hack for another tooltip position
dijit.showTooltip = function(/*String*/ innerHTML, /*DomNode*/ aroundNode, /*String[]?*/ position){
	// summary:
	//		Display tooltip w/specified contents in specified position.
	//		See description of dijit.Tooltip.defaultPosition for details on position parameter.
	//		If position is not specified then dijit.Tooltip.defaultPosition is used.
	position = ["BR-TR"];
	if(!dijit._masterTT){ dijit._masterTT = new dijit._MasterTooltip(); }
	return dijit._masterTT.show(innerHTML, aroundNode, position);
};
